package com.baida.retail.tasksystem.dao.impl;

import com.baida.retail.tasksystem.dao.UserDao;
import com.baida.retail.tasksystem.entity.User;
import com.baida.retail.tasksystem.enums.Role;
import com.baida.retail.tasksystem.mapper.ext.UserMapperExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class UserDaoImpl implements UserDao {
    @Autowired
    private UserMapperExt userMapperExt;

    @Override
    public List<User> getOnlineUsers(int repoId, Role role) {
        return userMapperExt.selectOnlineUsersBy(repoId, role.ordinal());
    }

    @Override
    public int setUserOnlineStatus(int uid, boolean isOnline) {
        User user = new User();
        user.setId(uid);
        user.setIsOnline(isOnline);
        if (isOnline) {
            user.setLastLoginTime(LocalDateTime.now());
        } else {
            user.setLastLogoutTime(LocalDateTime.now());
        }
        return userMapperExt.updateByPrimaryKeySelective(user);
    }

    @Override
    public User findByMobile(String mobile) {
        return userMapperExt.selectByMobile(mobile);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapperExt.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapperExt.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapperExt.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapperExt.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapperExt.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapperExt.updateByPrimaryKey(record);
    }

    @Override
    public User findByToken(String token) {
        return userMapperExt.selectByToken(token);
    }

    @Override
    public List<User> getAll() {
        return userMapperExt.getAll();
    }
}
